﻿const express = require('express')
const TextToSVG = require('text-to-svg')
const multer = require('multer')
const router = express.Router()
const path = require('path')
var upload = multer({ dest: path.resolve('./upload/') })

// 手机上传
router.post('/phoneupload', upload.single('file'), (req, res, next) => {
	let token = req.headers.token,
		body = req.body;
	let filePath = req.file.path;
	console.log(token, body, filePath)
	res.send('sfsdfsd')
})


// 生成
const production = function({SVG, obj, options}) {
	let svg = '', maxX = 0, width = 0, height = 0;
	//obj.arrList.forEach((item, i)=> {
		let box = SVG.getMetrics(obj.arrList, options)
		//options.y = box.ascender + box.descender
		let path = SVG.getPath(obj.arrList, options)
		
		 //console.log(box)
		maxX = maxX < Math.abs(box.x) ? Math.abs(box.x) : maxX;
		let g = `<g transform='translate(${-box.x},${ box.ascender + box.descender / 2})'>`
		g += path + '</g>'
		svg += g
		width = width < box.width ? width = box.width : width;
		width += 5;
		height += box.height
		// + box.descender
	//}){svg, width, height}
	return `${svg}&${width|0}&${height|0}`
}


router.get('/textToPath/pro', (req, res) => {
	let obj = {};

	obj.family = req.query.family || '华文宋体';
	
	

	if (obj.family.indexOf('站酷') > -1) {
		obj.family = '站酷快乐体'
	}	
	obj.align = req.query.align;
	obj.size = req.query.size;
	obj.spacing = req.query.spacing;
	obj.itailic = req.query.itailic;
	obj.bold = req.query.bold;
	obj.arrList = req.query.arrList;
	obj.line = req.query.line;
	obj.underline = req.query.underline;

	//console.log(req.query)
	if(obj == '') return res.send('传输为空')
	obj.align = 'top ' + obj.align
	let	options = { // 文字属性设置
			fontSize: obj.size, 
			//anchor: obj.align,
			letterSpacing: obj.spacing,
			attributes: {
				transform: obj.itailic == 1 ? 'translate(10, 0) skewX(-15)' : '',
				'stroke-width': obj.bold
			}
		};
	
		
	let	SVG = TextToSVG.loadSync(`./font/${obj.family}.ttf`);// 字体设置
	let result = production({SVG, obj, options})
	res.send(result)
	
})




module.exports = router;